package com.xcx.robot.mirai.mp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xcx.robot.mirai.entity.BaseEntity;
import com.xcx.robot.mirai.entity.team.Team;
import com.xcx.robot.mirai.entity.team.TeamInfo;
import com.xcx.robot.mirai.mp.mapper.TeamMapper;
import com.xcx.robot.mirai.mp.service.ITeamInfoService;
import com.xcx.robot.mirai.mp.service.ITeamService;
import net.mamoe.mirai.contact.Contact;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 邢晨旭
 * @since 2021-06-10
 */
@Service
public class TeamServiceImpl extends ServiceImpl<TeamMapper, Team> implements ITeamService {

    @Resource
    ITeamInfoService teamInfoService;

    @Override
    public Team getTeam(Contact receiver) {
        LambdaQueryWrapper<Team> teamQueryWrapper = Wrappers.lambdaQuery();
        teamQueryWrapper.eq(Team::getTeamGroup, receiver.getId());
        teamQueryWrapper.eq(Team::getDeleteYn, 0);
        return this.getOne(teamQueryWrapper);
    }

    @Override
    public void delTeam(Contact receiver) {
        Team team = this.getTeam(receiver);
        if (team == null) {
            return;
        }
        Team update1 = new Team();
        update1.setDeleteYn(1);
        LambdaUpdateWrapper<Team> wrapper1 = Wrappers.lambdaUpdate();
        wrapper1.eq(Team::getId, team.getId());
        this.update(update1,wrapper1);
        LambdaUpdateWrapper<TeamInfo> wrapper2 = Wrappers.lambdaUpdate();
        wrapper2.eq(TeamInfo::getId, team.getId());
        TeamInfo update2 = new TeamInfo();
        update2.setDeleteYn(1);
        teamInfoService.update(update2, wrapper2);
        ;
    }
}
